package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.button.MaterialButton;

public class ProductCategoryActivity extends AppCompatActivity {

    private MaterialButton btnAll, btnCart, btnPayment, btnReviews;

    private EditText searchEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_category);

        // 初始化按钮
        btnAll = findViewById(R.id.btn_all);
        btnCart = findViewById(R.id.btn_cart);
        searchEditText = findViewById(R.id.search_bar).findViewById(R.id.et_search);
        btnPayment = findViewById(R.id.btn_payment);
        btnReviews = findViewById(R.id.btn_reviews);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProductCategoryActivity.this, SearchHistoryActivity.class);
                startActivity(intent);
            }
        });

        // 默认加载商品列表Fragment
        replaceFragment(new ProductListFragment());

        // 按钮点击事件
        btnAll.setOnClickListener(v -> replaceFragment(new ProductListFragment()));
        btnCart.setOnClickListener(v -> replaceFragment(new TransactionFragment()));
        btnPayment.setOnClickListener(v -> replaceFragment(new PaymentFragment()));
        btnReviews.setOnClickListener(v -> replaceFragment(new ReviewFragment()));

        // 搜索框监听
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int after) {
                // 获取输入的搜索文本并过滤商品数据
                System.out.println("onTextChanged: " + charSequence.toString());
                Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
                // 判断当前Fragment是否为ProductListFragment
                if (currentFragment instanceof ProductListFragment) {    // 如果是，则调用ProductListFragment的filter方法
                    ((ProductListFragment) currentFragment).filter(searchEditText.getText().toString());
                } else {
                    // 如果不是，则调用TransactionFragment的filter方法
                    // 弹出警告
                    Toast.makeText(ProductCategoryActivity.this, "该功能需要进入商品列表页面", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 获取输入的搜索文本并过滤商品数据
             //   filterProducts(charSequence.toString());
                //
                // 获取当前Fragment
                System.out.println("afterTextChanged: " + s.toString());


            }

        });
    }




    // 过滤商品数据
    private void filterProducts(String query) {
       /* filteredList.clear();

        // 如果搜索框为空，显示所有商品
        if (query.isEmpty()) {
            filteredList.addAll(productList);
        } else {
            // 根据商品名和描述过滤
            for (Product product : productList) {
                if (product.getName().toLowerCase().contains(query.toLowerCase()) ||
                        product.getDescription().toLowerCase().contains(query.toLowerCase())) {
                    filteredList.add(product);
                }
            }
        }

        // 通知适配器数据已更新
        productAdapter.notifyDataSetChanged();*/
    }

    // 替换Fragment的方法
    private void replaceFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}
